package cn.zb.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description:Redis序列化器配置
 * @Author:
 * @Date: 2024/11/22 19:34
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory cf){
        RedisTemplate<String,Object> rt = new RedisTemplate<>();
        rt.setConnectionFactory(cf);
        // 创建序列化器
        StringRedisSerializer str = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer json = new GenericJackson2JsonRedisSerializer();
        // 设置
        rt.setKeySerializer(str);  rt.setHashKeySerializer(str);
        rt.setValueSerializer(json); rt.setHashValueSerializer(json);
        return rt;
    }
}
